import com.sun.prism.shader.DrawRoundRect_Color_AlphaTest_Loader;

import java.util.*;

public class t1172_3 {
    public static void main(String[] args) {
        String arr[] = {
                "DinnerPlates", "push", "push", "push", "push", "push", "push", "push", "push", "push", "push", "push", "push", "push", "push", "push", "push", "push", "push", "push", "push", "popAtStack", "popAtStack", "popAtStack", "popAtStack", "popAtStack", "popAtStack", "popAtStack", "popAtStack", "popAtStack", "popAtStack", "push", "push", "push", "push", "push", "push", "push", "push", "push", "push", "push", "push", "push", "push", "push", "push", "push", "push", "push", "push", "pop", "pop", "pop", "pop", "pop", "pop", "pop", "pop", "pop", "pop"
        };
        Integer num[] = {
                2, 373, 86, 395, 306, 370, 94, 41, 17, 387, 403, 66, 82, 27, 335, 252, 6, 269, 231, 35, 346, 4, 6, 2, 5, 2, 2, 7, 9, 8, 1, 474, 216, 256, 196, 332, 43, 75, 22, 273, 101, 11, 403, 33, 365, 338, 331, 134, 1, 250, 19, null, null, null, null, null, null, null, null, null, null
        };
        DinnerPlates dinnerPlates = null;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i].equals("DinnerPlates")) {
                dinnerPlates = new DinnerPlates(num[i]);
            } else if (arr[i].equals("push")) {
                dinnerPlates.push(num[i]);
            } else if (arr[i].equals("popAtStack")) {
                dinnerPlates.popAtStack(num[i]);
            } else {
                dinnerPlates.pop();
            }
            System.out.println(dinnerPlates.stack);
        }
    }
}

class DinnerPlates {
    List<Deque<Integer>> stack = new ArrayList<>();
    PriorityQueue<Integer> queue = new PriorityQueue<>();
    int max;

    public DinnerPlates(int capacity) {
        this.max = capacity;
    }

    public void push(int val) {
        if(!queue.isEmpty()&&queue.peek()>=stack.size()) queue.clear();
        if(queue.isEmpty()){
            Deque<Integer> deque = new ArrayDeque<>();
            deque.push(val);
            stack.add(deque);
            if(deque.size()<max)
                queue.add(stack.size()-1);
        }else{
            Integer index = queue.peek();
            Deque<Integer> deque = stack.get(index);
            deque.push(val);
           if(deque.size()==max) queue.poll();
        }
    }

    public int pop() {
        return popAtStack(stack.size()-1);
    }

    public int popAtStack(int index) {
        if(index<0||index>=stack.size()||stack.get(index).isEmpty()){
            return -1;
        }
        Deque<Integer> deque = stack.get(index);
        if(deque.size()==max)
            queue.add(index);
        Integer pop = deque.pop();
        while (!stack.isEmpty()&&stack.get(stack.size()-1).isEmpty()){
            stack.remove(stack.size()-1);
        }
        return pop;
    }
}

